﻿using CallCenter.Data.Base;
using CallCenter.Data.BizUtilities.Base;
using CallCenter.Data.Models;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;


namespace CallCenter.Data.BizUtilities
{
    public class BusinessUtility: BaseUtility
    {
        public BusinessUtility(DataManager manager)
            :base(manager)
        { }
        public ReturnMessage<BusinessViewModel> InsertToBusinessGroup(BusinessViewModel model)
        {
            var returnMessage = new ReturnMessage<BusinessViewModel>();
            //  if(this.ValidateScratchPadModel(model))
            //   {
            var id = this.Insert(model.Name);
            if (id == -1)
            {
                returnMessage.Message = "We cannot insert data to database";
                returnMessage.Status = false;
            }
            else
            {
                model.Id = id;
                returnMessage.Status = true;
            }
            return returnMessage;
        }

        public ReturnMessage<BusinessViewModel> UpdateToBusinessGroup(BusinessViewModel model)
        {
            var returnMessage = new ReturnMessage<BusinessViewModel>();
            //  if(this.ValidateScratchPadModel(model))
            //   {
            var id = this.Update(model.Id,model.Name);
            if (id == -1)
            {
                returnMessage.Message = "We cannot insert data to database";
                returnMessage.Status = false;
            }
            else
            {
                model.Id = id;
                returnMessage.Status = true;
            }
            return returnMessage;
        }


        public List<BusinessViewModel> GetAll() //Gets all the data form the contacts table
        {
            string sql = "Select * from ddlBizGroupTypes";

            return this.Factory.ExecuteObjects<BusinessViewModel>(sql);
        }
        public int Insert(string name)
        {
            int nRtn = -1;
            string sql = "INSERT INTO ddlBizGroupTypes(Name) Values(@Name) SELECT @pResult =@@IDENTITY";

            using (var cmd = this.Factory.GetCommand(sql))
            {
                this.Factory.AddParameter(cmd, "@Name", SqlDbType.NChar, 10, name);
               

                var param = this.Factory.AddParameter(cmd, "@pResult", System.Data.SqlDbType.NVarChar, 500, null);
                param.Direction = System.Data.ParameterDirection.Output;

                this.Factory.ExecuteNonQuery(cmd);
                nRtn = Convert.ToInt32(param.Value);
            }
            return nRtn;

        }
        public int Update(int id, string name) //insert data to a title table
        {
            int nRtn = -1;
            string sql = "UPDATE ddlBizGroupTypes SET Name=@Name WHERE id=@id";

            using (var cmd = this.Factory.GetCommand(sql))
            {
                this.Factory.AddParameter(cmd, "@Name", SqlDbType.NVarChar, 100, name);
                this.Factory.AddParameter(cmd, "@id", SqlDbType.Int, 10, id);
                this.Factory.ExecuteNonQuery(cmd);

            }
            nRtn = id;
            return nRtn; //retruning the data stored in the table
        }


        public void Delete(int titleId) //delete the row of data using the id passed to the function
        {
            string sql = "DELETE FROM ddlBizGroupTypes " +
                  "WHERE ID = @ID ";


            using (var cmd = this.Factory.GetCommand(sql))
            {
                this.Factory.AddParameter(cmd, "@ID", SqlDbType.Int, 10, titleId);
                this.Factory.ExecuteNonQuery(cmd);
            }

        }

        public void DeleteAll() //delete all the rows in the data.
        {
            string sql = "DELETE FROM ddlBizGroupTypes ";
            this.Factory.ExecuteTable(sql);

        }


    }
}
